package mis.ds.ps.ui.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import mis.ds.ps.controller.Result;
import mis.ds.ps.model.iface.Entities;

public class Step2 extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JTabbedPane tabbedPane;
	private ISwingAdapter swingAdapter;
	private Result result;

	public Step2(ISwingAdapter sa) {
		swingAdapter = sa;
		initialize();
	}

	private void initialize() {

	}

	public void actionPerformed(ActionEvent e) {

	}

	public void setResult(Result result) {

		if (tabbedPane != null) {
			remove(tabbedPane);
		}

		tabbedPane = new JTabbedPane();
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

		if (result.getEntity().equalsIgnoreCase(Entities.DOCENTES.toString())) {

			// extract avps
			Map<String, LinkedList<Object>> map = result.getValueMap();
			Object[] nomes = map.get("nome").toArray();

			tabbedPane.addTab("Docentes", new DocentesTable(nomes));
		}

		tabbedPane.addTab("Disciplinas", new DisciplinasTable());
		// tabbedPane.addTab("Salas", new CitiesPanel());
		// tabbedPane.addTab("Turmas", new CitiesPanel());
		// tabbedPane.addTab("...", new CitiesPanel());

		add(tabbedPane, BorderLayout.CENTER);
	}

	public Result getResult() {
		return result;
	}

}

class DocentesTable extends JPanel implements ActionListener {

	private static final long serialVersionUID = 9057101379054858536L;
	private JTable table; 
	private JScrollPane scrollPane;
	private JPopupMenu popupMenu;
	private JMenuItem menuItemInsert;
	private JMenuItem menuItemDelete;
	private MouseListener popupListener;

	private static String INSERT_CMD = "Insert Rows";
	private static String DELETE_CMD = "Delete Rows";

	public DocentesTable(Object[] nomes) {

		super(new GridLayout(1, 0));

		String[] columnNames = { "Versão", "Código", "Sigla", "Nome" };

		Object[][] data = new String[nomes.length][4];
		for (int j = 0; j < data.length; j++) {
			data[j][3] = nomes[j].toString();
		}

		table = new JTable();
		table.setModel(new DefaultTableModel(
        		data, columnNames));

		table.setPreferredScrollableViewportSize(new Dimension(750, 470));
		table.setFillsViewportHeight(true);

		// Create the scroll pane and add the table to it.
		scrollPane = new JScrollPane(table);

		// Add the scroll pane to this panel.
		add(scrollPane);
		addPopupMenu(table);
	}
	
	public void addPopupMenu(JTable table) {
		popupMenu = new JPopupMenu();
		menuItemInsert = new JMenuItem(INSERT_CMD);
		menuItemInsert.addActionListener(this);
	    popupMenu.add(menuItemInsert);
	    menuItemDelete = new JMenuItem(DELETE_CMD);
	    menuItemDelete.addActionListener(this);
	    popupMenu.add(menuItemDelete);
	    
	    popupListener = new PopupListener();
	    table.addMouseListener(popupListener);
	}
	
	class PopupListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			showPopup(e);
		}
		public void mouseReleased(MouseEvent e) {
			showPopup(e);
		}
		private void showPopup(MouseEvent e) {
			if (e.isPopupTrigger()) {
				popupMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());
		DefaultTableModel model = (DefaultTableModel) this.table.getModel();
		if (e.getActionCommand() == INSERT_CMD) {
			int row = table.getSelectedRow();
			model.insertRow(row+1,new Object[]{"","","",""});
		} else if (e.getActionCommand() == DELETE_CMD) {
			int[] rows = table.getSelectedRows();
			for(int i=0;i<rows.length;i++){
				model.removeRow(rows[i]-i);
			}
		} 
	}

}

class DisciplinasTable extends JPanel {

	private static final long serialVersionUID = 9057101379054858536L;

	public DisciplinasTable() {

		super(new GridLayout(1, 0));

		String[] columnNames = { "Versão", "Código", "Sigle", "Nome" };

		Object[][] data = { { "Kathy", "Smith", "Snowboarding", "" }, { "John", "Doe", "Rowing", "" }, { "Sue", "Black", "Knitting", "" },
				{ "Jane", "White", "Speed reading", "" }, { "Joe", "Brown", "Pool", "" } };

		final JTable table = new JTable(data, columnNames);

		table.setPreferredScrollableViewportSize(new Dimension(750, 470));
		table.setFillsViewportHeight(true);

		// Create the scroll pane and add the table to it.
		JScrollPane scrollPane = new JScrollPane(table);

		// Add the scroll pane to this panel.
		add(scrollPane);
	}
}
